The purpose of this document is to demonstrate some skills I have got in R during my course of “Programming AI with R” in the MBA Artificial Intelligence and Machine Learning given by FIAP University in 2019.
Built with 3.6.2
## [1] "/Users/arodrigues/Google Drive/FIAP-MBA-8IA/to_git/R"
informacao sobre funcoes
## Error in choose.files(): could not find function "choose.files"
instalar pacotes
## Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
## Error: package 'ggplot2' is required by 'plotly' so will not be detached
## Removing package from '/Library/Frameworks/R.framework/Versions/3.6/Resources/library'
## (as 'lib' is unspecified)
## [1] 3.14
## [1] "numeric"
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
vet_num <- c(1,2,3,4)
#exercicio 2
vetor_a <- c(1,2,3,4)
vetor_b <- c(5,6,7,8)
vetor_c <- vetor_a + vetor_bidades <- c(34,25,31,17)
names(idades)[1] <- "Wilian"
names(idades) <- c("Wilian","Marcelo","Lucas","Rafael")
idades## Wilian Marcelo Lucas Rafael
## 34 25 31 17
## Marcelo
## 25
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
## [1] 1 2 3
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## [1] "data.frame"
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
## [1] 11
## [1] 32
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
## Error in eval(expr, envir, enclos): cannot open file '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/ggplot2/data/Rdata.rdb': No such file or directory
## Warning in head(diamonds): restarting interrupted promise evaluation
## Error in head(diamonds): cannot open file '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/ggplot2/data/Rdata.rdb': No such file or directory
## Error: You're passing a function as global data.
## Have you misspelled the `data` argument in `ggplot()`
## Warning: restarting interrupted promise evaluation
## Error in eval(expr, envir, enclos): cannot open file '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/ggplot2/data/Rdata.rdb': No such file or directory
## $mpg
## [1] "numeric"
##
## $cyl
## [1] "numeric"
##
## $disp
## [1] "numeric"
##
## $hp
## [1] "numeric"
##
## $drat
## [1] "numeric"
##
## $wt
## [1] "numeric"
##
## $qsec
## [1] "numeric"
##
## $vs
## [1] "numeric"
##
## $am
## [1] "numeric"
##
## $gear
## [1] "numeric"
##
## $carb
## [1] "numeric"
## [,1] [,2] [,3] [,4]
## [1,] 2 10 18 26
## [2,] 4 12 20 28
## [3,] 6 14 22 30
## [4,] 8 16 24 32
## [,1] [,2] [,3] [,4]
## [1,] 2 10 18 26
## [2,] 4 12 20 28
## [3,] 6 14 22 30
## [4,] 8 16 24 32
df1 <- data.frame(row.names = c("L1","L2","L3","L4")
, nome_vetor_1 = v1
, nome_vetor_2 = v2
, nome_vetor_3 = v3
, nome_vetor_4 = v4
, nome_vetor_5 = v5)## Error in data.frame(row.names = c("L1", "L2", "L3", "L4"), nome_vetor_1 = v1, : object 'v1' not found
# path <- "http://raw.githubusercontent.com/elthonf/fiap-mba-r/master/"
# source(file = paste0(path, 'gereral_codes/inst_rmarkdown.r'))vetor_1 <- 1:9
vetor_2 <- 80:32
vetor_3 <- 4:-2
vetor_4 <- 3:3
vetor_5 <- seq(1,9)
identical(vetor_1, vetor_5)## [1] TRUE
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
## [1] 0.00000000 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545
## [7] 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1.00000000
## [1] 8 10
## [1] 1 2 3
## [1] 1 2 3 4 5
## [1] 0 0 0 0 0
## [1] 1 2 3 1 2 3 1 2 3
## [1] 1 1 1 2 2 2 3 3 3
## [1] 12 12 12 12
## [1] 1 2 3 1 2 3 1
## [1] 30
## [1] 10 9 8 7 6 5 4
## [1] 3.141593 4.141593 5.141593 6.141593 7.141593 8.141593 9.141593
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30
## [1] 1 2 3 NA
## [1] 1 2 3
## [1] "integer"
## [1] NA
Name <- c("joão","maria","diego")
Sex <- c("male",NA,"male")
Age <- c(45,NA, NA)
dt <- data.frame(Name, Sex, Age)
dt## Name Sex Age
## 1 joão male 45
## 2 maria <NA> NA
## 3 diego male NA
## Name Sex Age
## [1,] FALSE FALSE FALSE
## [2,] FALSE TRUE TRUE
## [3,] FALSE FALSE TRUE
## [1] 3
## [1] TRUE
## [1] 1 2 3 4 5 6 7 8 9
## [1] 2 3 4 5
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## [1] 6
## [1] 6 6 4 6 8
## mpg cyl disp hp drat
## Mazda RX4 21.0 6 160 110 3.90
## Mazda RX4 Wag 21.0 6 160 110 3.90
## Datsun 710 22.8 4 108 93 3.85
## Hornet 4 Drive 21.4 6 258 110 3.08
## Hornet Sportabout 18.7 8 360 175 3.15
## mpg cyl hp
## Mazda RX4 21.0 6 110
## Mazda RX4 Wag 21.0 6 110
## Datsun 710 22.8 4 93
## Hornet 4 Drive 21.4 6 110
## Hornet Sportabout 18.7 8 175
## mpg cyl hp
## Mazda RX4 21 6 110
## $a
## [1] 1 2 3
## [1] "list"
## [1] 1 2 3
## [1] 1 2 3
## Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
## Error in library(sqldf): there is no package called 'sqldf'
## Error in sqldf("select cyl,hp from mtcars"): could not find function "sqldf"
dia_texto <- "19/02/1990 T 07:45:30"
dia_date <- as.Date(dia_texto, "%d/%m/%Y T %H:%M:%S", tz= "America/Sao_Paulo")
dia.time1 <- as.POSIXct(dia_texto, format = "%d/%m/%Y T %H:%M:%S", tz= "America/Sao_Paulo")
dia.time1## [1] "1990-02-19 07:45:30 -03"
## [1] 635424330
## attr(,"tzone")
## [1] "America/Sao_Paulo"
dia.time2 <- as.POSIXlt(dia_texto, format = "%d/%m/%Y T %H:%M:%S", tz= "America/Sao_Paulo")
unclass(dia.time2)## $sec
## [1] 30
##
## $min
## [1] 45
##
## $hour
## [1] 7
##
## $mday
## [1] 19
##
## $mon
## [1] 1
##
## $year
## [1] 90
##
## $wday
## [1] 1
##
## $yday
## [1] 49
##
## $isdst
## [1] 0
##
## $zone
## [1] "-03"
##
## $gmtoff
## [1] NA
##
## attr(,"tzone")
## [1] "America/Sao_Paulo"
## [1] 1
## Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
## Error in library(lubridate): there is no package called 'lubridate'
## Error in ymd("20110604"): could not find function "ymd"
## Error in eval(expr, envir, enclos): object 'lub_date1' not found
## [1] "2011-06-04"
## Error in identical(lub_date1, date_1): object 'lub_date1' not found
## Error in dmy("01-02-1988"): could not find function "dmy"
## Error in eval(expr, envir, enclos): object 'lub_date2' not found
## Error in now(): could not find function "now"
## Error in today(): could not find function "today"
## Error in eval(expr, envir, enclos): object 'lub_date2' not found
## Error in eval(expr, envir, enclos): object 'lub_date2' not found
## Error in eval(expr, envir, enclos): object 'lub_date3' not found
## Error in as.duration(260): could not find function "as.duration"
## Error in duration(num = 260, units = "minutes"): could not find function "duration"
## Error in eval(expr, envir, enclos): object 'lub2' not found
## Error in duration(num = 1, units = "days"): could not find function "duration"
## Error in eval(expr, envir, enclos): object 'lub3' not found
## Error in ddays(x = 12): could not find function "ddays"
## Error in eval(expr, envir, enclos): object 'lub4' not found
## Error in dmy("01-11-2019"): could not find function "dmy"
## Error in eval(expr, envir, enclos): object 'dtentrada' not found
## Error in wday(dtentrada): could not find function "wday"
#criando criterios para avaliar o que fazer ao descobrir o dia da semana
# Opcoes (coordenadas pelos dois ifelse`s`)
#Se igual a 5 (quinta-feira), a variavel bf_check recebe 0 - como ? quinta n?o preciso mudar
#Se maior que 5 - 6 ou 7 - a variavel recebe 12 menos o wday(dtentrada) (6 ou 7) - como ? maior que 5 adiciono o que falta para a pr?xima quinta-feira
#Se menor que 5 - 1,2,3,4 - a variavel recebe 5 menos o wday(dtentrada) (1,2,3 ou 4) como ? menor que 5, adiciono o que falta para ser 5
bf_check <- ifelse(wday(dtentrada) == 5, 0,
ifelse(wday(dtentrada)>5, 12-wday(dtentrada), 5-wday(dtentrada)))## Error in wday(dtentrada): could not find function "wday"
## Error in eval(expr, envir, enclos): object 'bf_check' not found
# 3 semanas para chegar na quarta semana de novembro (dweeks(3))
# 1 dia para sair da quinta-feira e ir para sexta-feira da BF (ddays(1))
# correcao para o dia da semana como explicado acima (ddays(bf_check))
bf <- dtentrada + dweeks(3) + ddays(1) + ddays(bf_check) ## Error in eval(expr, envir, enclos): object 'dtentrada' not found
## Error in eval(expr, envir, enclos): object 'bf' not found
## [1] 334911 19760626 759 96181 7843
## [1] 669822 39521252 1518 192362 15686
## [1] 223274.000 13173750.667 506.000 64120.667 5228.667
## [1] 1.121656e+11 3.904824e+14 5.765870e+05 9.250849e+09 6.151788e+07
## [1] 1.121656e+11 3.904824e+14 5.765870e+05 9.250849e+09 6.151788e+07
## [1] 1.121654e+11 3.904823e+14 5.760810e+05 9.250785e+09 6.151265e+07
## [1] 334911 19760626 759 96181 7843
## [1] 5.608269e+10 1.952412e+14 2.880405e+05 4.625392e+09 3.075632e+07
## [1] 7843 96181 759 19760626 334911
## [1] 7843 96181 759 19760626 334911
## Warning in read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")):
## cannot open compressed file '/Library/Frameworks/R.framework/Versions/3.6/
## Resources/library/ggplot2/DESCRIPTION', probable reason 'No such file or
## directory'
## Warning in find.package(if (is.null(package)) loadedNamespaces() else package, :
## there is no package called 'ggplot2'
## Error: <text>:2:0: unexpected end of line
## 1: base::
## ^
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [4,] 7 8
## [,1] [,2] [,3] [,4] [,5]
## [1,] 334911 1.976063e+07 759.0 9.618100e+04 7843.000
## [2,] 669822 3.952125e+07 1518.0 1.923620e+05 15686.000
## [3,] 223274 1.317375e+07 506.0 6.412067e+04 5228.667
## [4,] 112165377921 3.904823e+14 576081.0 9.250785e+09 61512649.000
## [5,] 56082688960 1.952412e+14 288040.5 4.625392e+09 30756324.500
## [6,] 7843 9.618100e+04 759.0 1.976063e+07 334911.000
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 334911 669822 223274.000 1.121654e+11 5.608269e+10 7843
## [2,] 19760626 39521252 13173750.667 3.904823e+14 1.952412e+14 96181
## [3,] 759 1518 506.000 5.760810e+05 2.880405e+05 759
## [4,] 96181 192362 64120.667 9.250785e+09 4.625392e+09 19760626
## [5,] 7843 15686 5228.667 6.151265e+07 3.075632e+07 334911
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.349110e+04 1.976063e+06 75.90 9.618100e+03 784.3000
## [2,] 6.698220e+04 3.952125e+06 151.80 1.923620e+04 1568.6000
## [3,] 2.232740e+04 1.317375e+06 50.60 6.412067e+03 522.8667
## [4,] 1.121654e+10 3.904823e+13 57608.10 9.250785e+08 6151264.9000
## [5,] 5.608269e+09 1.952412e+13 28804.05 4.625392e+08 3075632.4500
## [6,] 7.843000e+02 9.618100e+03 75.90 1.976063e+06 33491.1000
## rating complaints privileges learning raises critical
## [1,] 67 61 45 47 62 80
## [2,] 64 53 53 58 58 67
## [3,] 67 60 47 39 59 74
## [4,] 69 62 57 42 55 63
## [5,] 68 83 83 45 59 77
## [1] "data.frame"
## [1] 30 7
## rating complaints privileges learning raises critical advance
## 1 43 51 30 39 61 92 45
## 2 63 64 51 54 63 73 47
## 3 71 70 68 69 76 86 48
## 4 61 63 45 47 54 84 35
## 5 81 78 56 66 71 83 47
## 6 43 55 49 44 54 49 34
## 7 58 67 42 56 66 68 35
## 8 71 75 50 55 70 66 41
## 9 72 82 72 67 71 83 31
## 10 67 61 45 47 62 80 41
## 11 64 53 53 58 58 67 34
## 12 67 60 47 39 59 74 41
## 13 69 62 57 42 55 63 25
## 14 68 83 83 45 59 77 35
## 15 77 77 54 72 79 77 46
## 16 81 90 50 72 60 54 36
## 17 74 85 64 69 79 79 63
## 18 65 60 65 75 55 80 60
## 19 65 70 46 57 75 85 46
## 20 50 58 68 54 64 78 52
## 21 50 40 33 34 43 64 33
## 22 64 61 52 62 66 80 41
## 23 53 66 52 50 63 80 37
## 24 40 37 42 58 50 57 49
## 25 63 54 42 48 66 75 33
## 26 66 77 66 63 88 76 72
## 27 78 75 58 74 80 78 49
## 28 48 57 44 45 51 83 38
## 29 85 85 71 71 77 74 55
## 30 82 82 39 59 64 78 39
## rating complaints privileges learning raises critical advance
## [1,] 43 51 30 39 61 92 45
## [2,] 63 64 51 54 63 73 47
## [3,] 71 70 68 69 76 86 48
## [4,] 61 63 45 47 54 84 35
## [5,] 81 78 56 66 71 83 47
## [6,] 43 55 49 44 54 49 34
## [7,] 58 67 42 56 66 68 35
## [8,] 71 75 50 55 70 66 41
## [9,] 72 82 72 67 71 83 31
## [10,] 67 61 45 47 62 80 41
## [11,] 64 53 53 58 58 67 34
## [12,] 67 60 47 39 59 74 41
## [13,] 69 62 57 42 55 63 25
## [14,] 68 83 83 45 59 77 35
## [15,] 77 77 54 72 79 77 46
## [16,] 81 90 50 72 60 54 36
## [17,] 74 85 64 69 79 79 63
## [18,] 65 60 65 75 55 80 60
## [19,] 65 70 46 57 75 85 46
## [20,] 50 58 68 54 64 78 52
## [21,] 50 40 33 34 43 64 33
## [22,] 64 61 52 62 66 80 41
## [23,] 53 66 52 50 63 80 37
## [24,] 40 37 42 58 50 57 49
## [25,] 63 54 42 48 66 75 33
## [26,] 66 77 66 63 88 76 72
## [27,] 78 75 58 74 80 78 49
## [28,] 48 57 44 45 51 83 38
## [29,] 85 85 71 71 77 74 55
## [30,] 82 82 39 59 64 78 39
## [1] "F" "M" "F" "M" "F" "M" "F" "M" "F" "M"
## [1] F M F M F M F M F M
## Levels: F M
## Warning in matrix(c(1, 2), ncol = 19, nrow = 19): data length [2] is not a sub-
## multiple or multiple of the number of rows [19]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [2,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [3,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [4,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [5,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [6,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [7,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [8,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [9,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [10,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [11,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [12,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [13,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [14,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [15,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [16,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [17,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [18,] 2 1 2 1 2 1 2 1 2 1 2 1 2
## [19,] 1 2 1 2 1 2 1 2 1 2 1 2 1
## [,14] [,15] [,16] [,17] [,18] [,19]
## [1,] 2 1 2 1 2 1
## [2,] 1 2 1 2 1 2
## [3,] 2 1 2 1 2 1
## [4,] 1 2 1 2 1 2
## [5,] 2 1 2 1 2 1
## [6,] 1 2 1 2 1 2
## [7,] 2 1 2 1 2 1
## [8,] 1 2 1 2 1 2
## [9,] 2 1 2 1 2 1
## [10,] 1 2 1 2 1 2
## [11,] 2 1 2 1 2 1
## [12,] 1 2 1 2 1 2
## [13,] 2 1 2 1 2 1
## [14,] 1 2 1 2 1 2
## [15,] 2 1 2 1 2 1
## [16,] 1 2 1 2 1 2
## [17,] 2 1 2 1 2 1
## [18,] 1 2 1 2 1 2
## [19,] 2 1 2 1 2 1
## Error in eval(expr, envir, enclos): object 'm19' not found
## Error in eval(expr, envir, enclos): object 'M191' not found
## Error in eval(expr, envir, enclos): object 'm191' not found
## Error in eval(expr, envir, enclos): object 'M1919' not found
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.121654e+10 3.904823e+13 5.760810e+04 9.250785e+08 6.151265e+06
## [2,] 4.486615e+10 1.561929e+14 2.304324e+05 3.700314e+09 2.460506e+07
## [3,] 4.985128e+09 1.735477e+13 2.560360e+04 4.111460e+08 2.733896e+06
## [4,] 1.258107e+21 1.524765e+28 3.318693e+10 8.557702e+18 3.783806e+14
## [5,] 3.145268e+20 3.811911e+27 8.296733e+09 2.139425e+18 9.459515e+13
## [6,] 6.151265e+06 9.250785e+08 5.760810e+04 3.904823e+13 1.121654e+10
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.121654e+10 3.904823e+13 5.760810e+04 9.250785e+08 6.151265e+06
## [2,] 4.486615e+10 1.561929e+14 2.304324e+05 3.700314e+09 2.460506e+07
## [3,] 4.985128e+09 1.735477e+13 2.560360e+04 4.111460e+08 2.733896e+06
## [4,] 1.258107e+21 1.524765e+28 3.318693e+10 8.557702e+18 3.783806e+14
## [5,] 3.145268e+20 3.811911e+27 8.296733e+09 2.139425e+18 9.459515e+13
## [6,] 6.151265e+06 9.250785e+08 5.760810e+04 3.904823e+13 1.121654e+10
## rating complaints privileges learning raises
## [1,] 1.294340e+09 1.074402e+09 1.068553e+09 1.166279e+09 1.172678e+09
## [2,] 2.588680e+09 2.148805e+09 2.137106e+09 2.332559e+09 2.345357e+09
## [3,] 8.628932e+08 7.162683e+08 7.123688e+08 7.775195e+08 7.817855e+08
## [4,] 2.499903e+16 2.070298e+16 2.070114e+16 2.265364e+16 2.265544e+16
## [5,] 1.249951e+16 1.035149e+16 1.035057e+16 1.132682e+16 1.132772e+16
## [6,] 1.392989e+09 1.258578e+09 1.159639e+09 8.509940e+08 1.112704e+09
## critical
## [1,] 1.357474e+09
## [2,] 2.714949e+09
## [3,] 9.049829e+08
## [4,] 2.617188e+16
## [5,] 1.308594e+16
## [6,] 1.277835e+09
## [,1] [,2] [,3] [,4] [,5]
## [1,] 8.748973e+12 3.045763e+16 45161259 723163125077 4826497119
## [2,] 9.758457e+12 3.397197e+16 50325748 806161985314 5375649929
## [3,] 7.683402e+12 2.674804e+16 39683430 635162041971 4240112259
## [4,] 7.795572e+12 2.713853e+16 40260776 644196678138 4298040232
## [5,] 8.356418e+12 2.909094e+16 43196082 690732861266 4610749739
## [,1] [,2] [,3] [,4] [,5]
## [1,] TRUE TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE TRUE
## [5,] TRUE TRUE TRUE TRUE TRUE
## [6,] TRUE TRUE TRUE TRUE TRUE
## Error in M7 == M8: non-conformable arrays
## rating complaints privileges learning raises critical advance
## [1,] 43 51 30 39 61 92 45
## [2,] 63 64 51 54 63 73 47
## [3,] 71 70 68 69 76 86 48
## [4,] 61 63 45 47 54 84 35
## [5,] 81 78 56 66 71 83 47
## [6,] 43 55 49 44 54 49 34
## [7,] 58 67 42 56 66 68 35
## [8,] 71 75 50 55 70 66 41
## [9,] 72 82 72 67 71 83 31
## [10,] 67 61 45 47 62 80 41
## [11,] 64 53 53 58 58 67 34
## [12,] 67 60 47 39 59 74 41
## [13,] 69 62 57 42 55 63 25
## [14,] 68 83 83 45 59 77 35
## [15,] 77 77 54 72 79 77 46
## [16,] 81 90 50 72 60 54 36
## [17,] 74 85 64 69 79 79 63
## [18,] 65 60 65 75 55 80 60
## [19,] 65 70 46 57 75 85 46
## [20,] 50 58 68 54 64 78 52
## [21,] 50 40 33 34 43 64 33
## [22,] 64 61 52 62 66 80 41
## [23,] 53 66 52 50 63 80 37
## [24,] 40 37 42 58 50 57 49
## [25,] 63 54 42 48 66 75 33
## [26,] 66 77 66 63 88 76 72
## [27,] 78 75 58 74 80 78 49
## [28,] 48 57 44 45 51 83 38
## [29,] 85 85 71 71 77 74 55
## [30,] 82 82 39 59 64 78 39
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## rating 43 63 71 61 81 43 58 71 72 67 64 67 69
## complaints 51 64 70 63 78 55 67 75 82 61 53 60 62
## privileges 30 51 68 45 56 49 42 50 72 45 53 47 57
## learning 39 54 69 47 66 44 56 55 67 47 58 39 42
## raises 61 63 76 54 71 54 66 70 71 62 58 59 55
## critical 92 73 86 84 83 49 68 66 83 80 67 74 63
## advance 45 47 48 35 47 34 35 41 31 41 34 41 25
## [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
## rating 68 77 81 74 65 65 50 50 64 53 40
## complaints 83 77 90 85 60 70 58 40 61 66 37
## privileges 83 54 50 64 65 46 68 33 52 52 42
## learning 45 72 72 69 75 57 54 34 62 50 58
## raises 59 79 60 79 55 75 64 43 66 63 50
## critical 77 77 54 79 80 85 78 64 80 80 57
## advance 35 46 36 63 60 46 52 33 41 37 49
## [,25] [,26] [,27] [,28] [,29] [,30]
## rating 63 66 78 48 85 82
## complaints 54 77 75 57 85 82
## privileges 42 66 58 44 71 39
## learning 48 63 74 45 71 59
## raises 66 88 80 51 77 64
## critical 75 76 78 83 74 78
## advance 33 72 49 38 55 39
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 1 0 0 0 0 0 0 0 0
## [2,] 0 2 0 0 0 0 0 0 0
## [3,] 0 0 3 0 0 0 0 0 0
## [4,] 0 0 0 4 0 0 0 0 0
## [5,] 0 0 0 0 5 0 0 0 0
## [6,] 0 0 0 0 0 6 0 0 0
## [7,] 0 0 0 0 0 0 7 0 0
## [8,] 0 0 0 0 0 0 0 8 0
## [9,] 0 0 0 0 0 0 0 0 9
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5 0 0 0 0
## [2,] 0 5 0 0 0
## [3,] 0 0 5 0 0
## [4,] 0 0 0 5 0
## [5,] 0 0 0 0 5
## [6,] 0 0 0 0 0
## [7,] 0 0 0 0 0
## [8,] 0 0 0 0 0
## [9,] 0 0 0 0 0
## [10,] 0 0 0 0 0
# Matriz Identidade:
## Error in M %*% I = M: could not find function "%*%<-"
## rating complaints privileges learning raises
## 21 50 40 33 34 43
## 22 64 61 52 62 66
## 23 53 66 52 50 63
## 24 40 37 42 58 50
## 25 63 54 42 48 66
## rating complaints privileges learning raises
## 11 64 53 53 58 58
## 12 67 60 47 39 59
## 13 69 62 57 42 55
## 14 68 83 83 45 59
## 15 77 77 54 72 79
## Warning in read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")):
## cannot open compressed file '/Library/Frameworks/R.framework/Versions/3.6/
## Resources/library/ggplot2/DESCRIPTION', probable reason 'No such file or
## directory'
## Warning in find.package(if (is.null(package)) loadedNamespaces() else package, :
## there is no package called 'ggplot2'
## rating complaints privileges learning raises
## rating 0.7389616 -0.1158204 0.2187957 1.173421 0.7785334
## complaints -4.6257572 -6.1397730 -8.1338315 -6.403933 -4.3351608
## privileges 6.8562975 7.6917856 12.2993714 7.961175 5.0382583
## learning -3.3515454 -3.6302589 -5.2339778 -4.925546 -2.9153565
## raises 2.3203939 4.0460887 3.2439410 3.726420 2.9148688
## rating complaints privileges learning raises
## rating 0.7389616 -0.1158204 0.2187957 1.173421 0.7785334
## complaints -4.6257572 -6.1397730 -8.1338315 -6.403933 -4.3351608
## privileges 6.8562975 7.6917856 12.2993714 7.961175 5.0382583
## learning -3.3515454 -3.6302589 -5.2339778 -4.925546 -2.9153565
## raises 2.3203939 4.0460887 3.2439410 3.726420 2.9148688
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
Não é muito informativo usar estado como fator.
Conta as qtdes das variá?veis.
## Error in table(BrFlights2$Pais.Origem): object 'BrFlights2' not found
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
## Error in factor(BrFlights2$Pais.Origem): object 'BrFlights2' not found
## Warning in read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")):
## cannot open compressed file '/Library/Frameworks/R.framework/Versions/3.6/
## Resources/library/ggplot2/DESCRIPTION', probable reason 'No such file or
## directory'
## Warning in find.package(if (is.null(package)) loadedNamespaces() else package, :
## there is no package called 'ggplot2'
## Sexo
## 1 F
## 2 M
## 3 F
## 4 M
## 5 F
## 6 M
## 7 F
## 8 M
## 9 F
## 10 M
## 11 F
## 12 M
## 13 F
## 14 M
## 15 F
## 16 M
## 17 F
## 18 M
## 19 F
## 20 M
## Sexo Sexo2
## 1 F F
## 2 M M
## 3 F F
## 4 M M
## 5 F F
## 6 M M
## 7 F F
## 8 M M
## 9 F F
## 10 M M
## 11 F F
## 12 M M
## 13 F F
## 14 M M
## 15 F F
## 16 M M
## 17 F F
## 18 M M
## 19 F F
## 20 M M
## Sexo Sexo2
## 1 F Feminino
## 2 M Masculino
## 3 F Feminino
## 4 M Masculino
## 5 F Feminino
## 6 M Masculino
## 7 F Feminino
## 8 M Masculino
## 9 F Feminino
## 10 M Masculino
## 11 F Feminino
## 12 M Masculino
## 13 F Feminino
## 14 M Masculino
## 15 F Feminino
## 16 M Masculino
## 17 F Feminino
## 18 M Masculino
## 19 F Feminino
## 20 M Masculino
## [1] Feminino Masculino Feminino Masculino Feminino Masculino Feminino
## [8] Masculino Feminino Masculino Feminino Masculino Feminino Masculino
## [15] Feminino Masculino Feminino Masculino Feminino Masculino
## Levels: Masculino Feminino
## Sexo Sexo2
## 1 F 2
## 2 M 1
## 3 F 2
## 4 M 1
## 5 F 2
## 6 M 1
## 7 F 2
## 8 M 1
## 9 F 2
## 10 M 1
## 11 F 2
## 12 M 1
## 13 F 2
## 14 M 1
## 15 F 2
## 16 M 1
## 17 F 2
## 18 M 1
## 19 F 2
## 20 M 1
## Error in nrow(BrFlights2): object 'BrFlights2' not found
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
## Error in eval(expr, envir, enclos): object 'atrasos' not found
## Warning in read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")):
## cannot open compressed file '/Library/Frameworks/R.framework/Versions/3.6/
## Resources/library/ggplot2/DESCRIPTION', probable reason 'No such file or
## directory'
## Warning in find.package(if (is.null(package)) loadedNamespaces() else package, :
## there is no package called 'ggplot2'
## [1] "O" "A" "U" "T" "R" "G" "I" "L" "N"
## [1] "R" "I" "T" "N" "N" "G" "U" "O" "N"
## [1] "N" "T" "L" "G" "O"
## [1] "A" "O" "O" "G" "O" "I" "A" "N" "U" "I"
## [1] 0.59390132 0.91897737 0.78213630 0.07456498 -1.98935170
## [1] -0.08458607 0.84040013 -0.46348277 -0.55083500 0.73604043
## [1] 153 6
## Ozone Solar.R Wind Temp
## Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00
## 1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00
## Median : 31.50 Median :205.0 Median : 9.700 Median :79.00
## Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88
## 3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00
## Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00
## NA's :37 NA's :7
## Month Day
## Min. :5.000 Min. : 1.0
## 1st Qu.:6.000 1st Qu.: 8.0
## Median :7.000 Median :16.0
## Mean :6.993 Mean :15.8
## 3rd Qu.:8.000 3rd Qu.:23.0
## Max. :9.000 Max. :31.0
##
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## Ozone Solar.R Wind Temp Month Day
## 148 14 20 16.6 63 9 25
## 149 30 193 6.9 70 9 26
## 150 NA 145 13.2 77 9 27
## 151 14 191 14.3 75 9 28
## 152 18 131 8.0 76 9 29
## 153 20 223 11.5 68 9 30
## [1] 1 2 3 8 6 4 7 9 10 5
## Ozone Solar.R Wind Temp Month Day
## 62 135 269 4.1 84 7 1
## 7 23 299 8.6 65 5 7
## 19 30 322 11.5 68 5 19
## 63 49 248 9.2 85 7 2
## 70 97 272 5.7 92 7 9
## 135 21 259 15.5 76 9 12
## 66 64 175 4.6 83 7 5
## 102 NA 222 8.6 92 8 10
## 121 118 225 2.3 94 8 29
## 8 19 99 13.8 59 5 8
set.seed(20)
linhas.idx <- seq_len(nrow(airquality))
linhas.sample <- sample(linhas.idx, 10)
airquality[linhas.sample,]## Ozone Solar.R Wind Temp Month Day
## 107 NA 64 11.5 79 8 15
## 120 76 203 9.7 97 8 28
## 130 20 252 10.9 80 9 7
## 98 66 NA 4.6 87 8 6
## 29 45 252 14.9 81 5 29
## 45 NA 332 13.8 80 6 14
## 127 91 189 4.6 93 9 4
## 41 39 323 11.5 87 6 10
## 67 40 314 10.9 83 7 6
## 121 118 225 2.3 94 8 29
## [1] 17 44 125 75 5 67 12 41 11 138
## Ozone Solar.R Wind Temp Month Day
## 151 14 191 14.3 75 9 28
## 10 NA 194 8.6 69 5 10
## 103 NA 137 11.5 86 8 11
## 51 13 137 10.3 76 6 20
## 69 97 267 6.3 92 7 8
## 127 91 189 4.6 93 9 4
## 29 45 252 14.9 81 5 29
## 78 35 274 10.3 82 7 17
## 73 10 264 14.3 73 7 12
## 71 85 175 7.4 89 7 10
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 39.41 69.88 79.17 79.94 90.04 130.52
## [1] 14.37424
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 17.07 69.87 79.91 79.98 90.15 140.94
## [1] 14.9562
## Warning in read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")):
## cannot open compressed file '/Library/Frameworks/R.framework/Versions/3.6/
## Resources/library/ggplot2/DESCRIPTION', probable reason 'No such file or
## directory'
## Warning in find.package(if (is.null(package)) loadedNamespaces() else package, :
## there is no package called 'ggplot2'
## [1] 0.1612813
Regressao Linear
## [1] 0.34079969 -0.70334030 -0.37953773 -0.74604744 -0.89810733 -0.33479411
## [7] -0.50137815 -0.17453570 1.80903740 -0.23010497 -1.13041822 0.21598889
## [13] 1.23223729 1.60935871 0.40155063 -0.27298403 -0.03615234 -0.15031123
## [19] 3.76881035 -1.65249598 -1.13514510 0.22767017 -0.18331854 -0.41351862
## [25] -0.43759528 -0.02618435 -0.85983418 0.16654458 1.47549073 0.19542291
## [31] 0.15942179 -0.72019328 -0.93550254 0.28543230 -0.73923515 0.42914898
## [37] 2.73398385 -1.33340333 1.86009535 0.24596992 -0.74598942 -1.48413799
## [43] 0.22204850 0.47782760 0.73119851 0.17210513 1.18669199 -0.35040940
## [49] 1.14759741 1.35008401 1.11614940 0.20234597 0.11149061 -1.52799033
## [55] 0.05750731 2.18746007 -0.07095490 1.29299658 0.37687501 -0.81651082
## [61] -0.07169406 -2.15850547 2.01919496 -0.05012938 -0.28021325 0.31783776
## [67] -0.96890304 -0.50034234 -0.96092400 -1.59004694 0.37573467 1.02919849
## [73] 1.22623137 1.04839459 0.16741396 1.13799896 1.05672549 -0.68015871
## [79] -1.33738964 -1.80310187 0.70097740 -2.04409051 -0.98805538 0.62738221
## [85] -0.38374189 3.60352286 0.33221058 -1.24505785 0.24736600 -1.40402871
## [91] 0.90718101 -0.70395328 -1.56977100 0.34531272 0.67993840 1.14271882
## [97] 0.40591523 -0.91864023 2.29631307 -0.68969735
## [1] -3.75659775 2.12868800 2.27665488 1.55921463 -1.26408291 0.37685427
## [7] -1.81937844 -0.27203422 0.45871256 -1.45026436 1.52226183 -0.15895636
## [13] 3.45498471 0.81040275 -2.80724337 0.13294523 0.61086046 1.74635528
## [19] -0.05789542 0.26125767 0.15133924 1.57233132 -3.25560339 0.15629099
## [25] -1.23012637 0.18654976 0.32920774 -2.14635557 -0.75096643 -0.03949354
## [31] 1.21682699 0.94841711 -3.38184620 1.84229132 1.04052067 0.87119741
## [37] -0.47713156 -2.16703385 1.56338695 -1.46717388 0.36607988 0.84414514
## [43] -0.05537081 0.91068011 2.51271079 -2.66212082 -1.77512795 -0.21009780
## [49] -0.18099061 -3.60214815 1.42049096 0.08042273 0.59671184 2.48660723
## [55] -0.70738315 -5.70157860 1.75746243 -4.09245356 3.28924385 -0.83720209
## [61] 1.83277720 0.72168868 4.40938464 1.73074613 2.99381836 1.34295120
## [67] -2.41623153 -1.08031926 -1.82448003 -2.12824670 -2.30630212 0.61938098
## [73] 0.23959470 1.23211873 -1.79065837 -3.52168224 -1.29713661 3.33156120
## [79] 3.66819053 -3.80747097 3.17767331 -1.02635257 1.68242327 -4.04963704
## [85] 1.71135381 1.06761329 0.10350465 -0.17619227 -1.78551998 -1.36731370
## [91] 1.89915756 -1.27262398 2.69238555 1.42654310 -0.56445959 -2.77163920
## [97] -3.05968686 -0.68812366 -2.60297193 0.05389678
## [1] -2.574998372 1.222007396 2.017579419 0.567119753 -2.560297571
## [6] 0.207266055 -2.322134749 -0.121105609 4.576787368 -1.410474305
## [11] -0.238574610 0.773021417 6.419459284 4.529120168 -1.504142116
## [16] 0.086977157 1.038555776 1.945732814 7.979725278 -2.543734284
## [21] -1.618950967 2.527671653 -3.122240463 -0.170746256 -1.605316934
## [26] 0.634181049 -0.890460614 -1.313266418 2.700015025 0.851352287
## [31] 2.035670567 0.008030544 -4.752851286 2.913155926 0.062050379
## [36] 2.229495367 5.490836135 -4.333840506 5.783577638 -0.475234033
## [41] -0.625898968 -1.624130836 0.888726185 2.366335306 4.475107810
## [46] -1.817910557 1.098256025 -0.410916597 2.614204214 -0.401980128
## [51] 4.152789756 0.985114679 1.319693057 -0.069373418 -0.092368523
## [56] -0.826658460 2.115552638 -1.006460405 4.542993862 -1.970223735
## [61] 2.189389085 -3.095322249 8.947774567 2.130487362 2.933391864
## [66] 2.478626715 -3.854037620 -1.581003936 -3.246328026 -4.808340581
## [71] -1.054832784 3.177777964 3.192057438 3.828907917 -0.955830442
## [76] -0.745684320 1.316314369 2.471243775 1.493411258 -6.913674722
## [81] 5.079628119 -4.614533581 0.206312505 -2.294872615 1.443870022
## [86] 8.774659015 1.267925816 -2.166307968 -0.790787968 -3.675371131
## [91] 4.213519592 -2.180530534 0.052843550 2.617168551 1.295417206
## [96] 0.013798435 -1.747856399 -2.025404127 2.489654218 -0.825497906
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning in read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")):
## cannot open compressed file '/Library/Frameworks/R.framework/Versions/3.6/
## Resources/library/ggplot2/DESCRIPTION', probable reason 'No such file or
## directory'
## Warning in find.package(if (is.null(package)) loadedNamespaces() else package, :
## there is no package called 'ggplot2'
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
## Error in nrow(BrFlights2): object 'BrFlights2' not found
## Error in table(BrFlights2$Pais.Origem): object 'BrFlights2' not found
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
## Error in is.factor(x): object 'BrFlights2' not found
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
## Error in eval(expr, envir, enclos): object 'BrFlights2' not found
## Error in which.max(BrFlights2$Atrasos): object 'BrFlights2' not found
# View(BrFlights2[registro.maior_atraso,])
# View(BrFlights2$Companhia.Aerea[registro.maior_atraso])
# View(BrFlights2[registro.maior_atraso, c(4, 9, 12:15)])## Error in print(strenght): object 'strenght' not found
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1949 112 118 132 129 121 135 148 148 136 119 104 118
## 1950 115 126 141 135 125 149 170 170 158 133 114 140
## 1951 145 150 178 163 172 178 199 199 184 162 146 166
## 1952 171 180 193 181 183 218 230 242 209 191 172 194
## 1953 196 196 236 235 229 243 264 272 237 211 180 201
## 1954 204 188 235 227 234 264 302 293 259 229 203 229
## 1955 242 233 267 269 270 315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
## 1957 315 301 356 348 355 422 465 467 404 347 305 336
## 1958 340 318 362 348 363 435 491 505 404 359 310 337
## 1959 360 342 406 396 420 472 548 559 463 407 362 405
## 1960 417 391 419 461 472 535 622 606 508 461 390 432
## [1] "ts"
# Série Temporal: Conjunto de frequecia, cuja cada observacao tem a mesma distancia de tempo,ou seja, cada observacao tem o mesmo periodo.
#View(AirPassengers)
ac <- 0
for ( billy in 1:length(AirPassengers)) {
if (billy == 1){
ac[billy] = AirPassengers[billy]
}else{
ac[billy] = ac[billy - 1] + AirPassengers[billy]
}
}
print(ac)## [1] 112 230 362 491 612 747 895 1043 1179 1298 1402 1520
## [13] 1635 1761 1902 2037 2162 2311 2481 2651 2809 2942 3056 3196
## [25] 3341 3491 3669 3832 4004 4182 4381 4580 4764 4926 5072 5238
## [37] 5409 5589 5782 5963 6146 6364 6594 6836 7045 7236 7408 7602
## [49] 7798 7994 8230 8465 8694 8937 9201 9473 9710 9921 10101 10302
## [61] 10506 10694 10929 11156 11390 11654 11956 12249 12508 12737 12940 13169
## [73] 13411 13644 13911 14180 14450 14765 15129 15476 15788 16062 16299 16577
## [85] 16861 17138 17455 17768 18086 18460 18873 19278 19633 19939 20210 20516
## [97] 20831 21132 21488 21836 22191 22613 23078 23545 23949 24296 24601 24937
## [109] 25277 25595 25957 26305 26668 27103 27594 28099 28503 28862 29172 29509
## [121] 29869 30211 30617 31013 31433 31905 32453 33012 33475 33882 34244 34649
## [133] 35066 35457 35876 36337 36809 37344 37966 38572 39080 39541 39931 40363
## [1] "a" "A" "ac" "Age"
## [5] "ai" "air" "amostra" "b"
## [9] "B" "b0" "b1" "billy"
## [13] "c" "C" "d" "date_1"
## [17] "df" "dia_date" "dia_texto" "dia.time1"
## [21] "dia.time2" "dt" "e" "i"
## [25] "idades" "linhas.idx" "linhas.sample" "lista"
## [29] "M" "M0" "m1" "M1"
## [33] "M19" "m2" "M2" "M3"
## [37] "M4" "M5" "M6" "M7"
## [41] "M8" "MT" "my_seq" "Name"
## [45] "peso" "Sex" "sexo" "strength"
## [49] "vet" "vet_num" "vet1" "vetor_1"
## [53] "vetor_2" "vetor_3" "vetor_4" "vetor_5"
## [57] "vetor_6" "vetor_7" "vetor_a" "vetor_b"
## [61] "vetor_c" "x" "y"
fatorial <- function(n){
prod = 1
while (n >= 1){
prod = prod * n
n = n - 1
}
return (prod)
}
fatorial(10)## [1] 3628800
## [1] 3628800
#setwd() -> Ajusta o diretorio de trabalho
#getwd() -> Obtem o diretorio de trabalho atual
#Caminho Relativo: "./data", "../"
#Caminho absoluto: "/r/fiap/data"
#Formato Windows: "C:\\r\\fiap\\data
#Boa pratica: Separar seus dados de analise dos scripts de analise# getwd()
#
# file.exists ('data')
#
# if(!file.exists ('data')){
# dir.create('data')
# }
#
# file.url = 'https://storage.googleapis.com/ds-publico/IE1-04.xlsx'
# file.local = file.path('./data', basename(file.url))
# download.file(url = file.url, destfile = file.local , mode='wb')
# basename(file.url)
# file.local
#
# download_url <- function(url1){
# if(!file.exists ('data')){
# dir.create('data')
# }
# file.url = url1
# print(paste("URL:", file.url))
# file.base = basename(file.url)
# file.dest = file.path('./data', file.base)
# print(paste("Destino:", file.dest))
# download.file(url = file.url, destfile = file.dest , mode='wb')
# }
#
# download_url('https://storage.googleapis.com/ds-publico/Copas.csv' )
# download_url('https://storage.googleapis.com/ds-publico/Copas-Partidas.csv' )
# download_url('https://storage.googleapis.com/ds-publico/Copas-Jogadores.csv' )# library(readr)
# Copas <- read_csv("data/Copas.csv")
# View(Copas)
#
# library(readr)
# Copas_Partidas <- read_csv("data/Copas-Partidas.csv")
# View(Copas_Partidas)
#
# library(readr)
# Copas_Jogadores <- read_csv("data/Copas-Jogadores.csv")
# View(Copas_Jogadores)# download_url('https://storage.googleapis.com/ds-publico/cameras.baltimore.xlsx')
#
# library(readxl)
# cameras_baltimore <- read_excel("data/cameras.baltimore.xlsx")
# View(cameras_baltimore)
#
# Cam_N_Lat <- which.max(cameras_baltimore$Lat)
# Cam_S_Lat <- which.min(cameras_baltimore$Lat)
# Cam_W_Long <- which.min(cameras_baltimore$Long)
# Cam_E_Long <- which.max(cameras_baltimore$Long)
#
# View(cameras_baltimore[Cam_N_Lat,])
# View(cameras_baltimore[Cam_S_Lat,])
# View(cameras_baltimore[Cam_E_Long,])
# View(cameras_baltimore[Cam_W_Long,])
#
# library(dplyr)
# View(starwars)
# View(starwars[which.max(starwars$mass),])
# s2 <- mutate (starwars, imc = mass/ ((height / 100) ^2))
# View(s2)
# View(starwars[which.max(s2$imc),])## Error in eval(lhs, parent, parent): object 'starwars' not found
## Error in eval(lhs, parent, parent): object 'starwars' not found
## Error in eval(lhs, parent, parent): object 'starwars' not found
## Error in head(starwars, .): object 'starwars' not found
## Error in eval(lhs, parent, parent): object 'starwars' not found
## Error in eval(lhs, parent, parent): object 'starwars' not found
## Warning in readChar(con, 5L, useBytes = TRUE): cannot open compressed file 'P:/
## 08IA/BrFlights2.RData', probable reason 'No such file or directory'
## Error in readChar(con, 5L, useBytes = TRUE): cannot open the connection
# View(BrFlights2)
# AzulFlights <- BrFlights2 %>% filter(Companhia.Aerea == "AZUL")
# View(AzulFlights)
# dim(AzulFlights)## Error in eval(lhs, parent, parent): object 'starwars' not found
AzulFlights <- BrFlights2 %>% filter(Companhia.Aerea == “AZUL”) %>%
select(Companhia.Aerea, Cidade.Origem, Cidade.Destino, Situacao.Voo)
AzulFlights <- BrFlights2 %>%
filter(Companhia.Aerea == "AZUL") %>%
select(Companhia.Aerea, Cidade.Origem, Cidade.Destino, Situacao.Voo)## Error in eval(lhs, parent, parent): object 'BrFlights2' not found
starwars %>% arrange(desc(mass))
# View(BrFlights2)
BrFlights2 %>% mutate(TempoAtrasado = Chegada.Real - Chegada.Prevista ) %>%
filter(Codigo.Tipo.Linha != "Internacional") %>%
arrange(desc(TempoAtrasado)) %>%
head(100) %>%
# View()## Error: <text>:10:0: unexpected end of input
## 8: head(100) %>%
## 9: # View()
## ^
## Error in rename(starwars, nome = name, altura = height, massa = mass): object 'starwars' not found
starwars %>%
group_by(species) %>%
summarise(j = n()) %>%
# View()
starwars %>%
group_by(species) %>%
summarise(
j = n(),
mass = mean(mass, na.rm = TRUE)
) %>%
filter(j > 1) %>%
# View()
BrFlights2 %>% mutate(TempoAtrasado = Chegada.Real - Chegada.Prevista,
DistEucl = sqrt((BrFlights2$LatOrig - BrFlights2$LatDest)^2 + (BrFlights2$LongOrig - BrFlights2$LongDest)^2)) %>%
arrange(desc(TempoAtrasado)) %>%
head(100) %>%
group_by(Companhia.Aerea) %>%
summarise(
j = n(),
atraso = mean(TempoAtrasado, na.rm = TRUE),
distancia = mean(DistEucl, na.rm = TRUE)
) %>%
# View()## Error: <text>:26:0: unexpected end of input
## 24: ) %>%
## 25: # View()
## ^
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
*Histograma
## Warning in read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")):
## cannot open compressed file '/Library/Frameworks/R.framework/Versions/3.6/
## Resources/library/ggplot2/DESCRIPTION', probable reason 'No such file or
## directory'
## Warning in find.package(if (is.null(package)) loadedNamespaces() else package, :
## there is no package called 'ggplot2'
*BoxPlots
## Ozone Solar.R Wind Temp
## Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00
## 1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00
## Median : 31.50 Median :205.0 Median : 9.700 Median :79.00
## Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88
## 3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00
## Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00
## NA's :37 NA's :7
## Month Day
## Min. :5.000 Min. : 1.0
## 1st Qu.:6.000 1st Qu.: 8.0
## Median :7.000 Median :16.0
## Mean :6.993 Mean :15.8
## 3rd Qu.:8.000 3rd Qu.:23.0
## Max. :9.000 Max. :31.0
##
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.00 18.00 31.50 42.13 63.25 168.00 37
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
## 7 23 299 8.6 65 5 7
## 8 19 99 13.8 59 5 8
## 9 8 19 20.1 61 5 9
## 10 NA 194 8.6 69 5 10
## 11 7 NA 6.9 74 5 11
## 12 16 256 9.7 69 5 12
## 13 11 290 9.2 66 5 13
## 14 14 274 10.9 68 5 14
## 15 18 65 13.2 58 5 15
## 16 14 334 11.5 64 5 16
## 17 34 307 12.0 66 5 17
## 18 6 78 18.4 57 5 18
## 19 30 322 11.5 68 5 19
## 20 11 44 9.7 62 5 20
## 21 1 8 9.7 59 5 21
## 22 11 320 16.6 73 5 22
## 23 4 25 9.7 61 5 23
## 24 32 92 12.0 61 5 24
## 25 NA 66 16.6 57 5 25
## 26 NA 266 14.9 58 5 26
## 27 NA NA 8.0 57 5 27
## 28 23 13 12.0 67 5 28
## 29 45 252 14.9 81 5 29
## 30 115 223 5.7 79 5 30
## 31 37 279 7.4 76 5 31
## 32 NA 286 8.6 78 6 1
## 33 NA 287 9.7 74 6 2
## 34 NA 242 16.1 67 6 3
## 35 NA 186 9.2 84 6 4
## 36 NA 220 8.6 85 6 5
## 37 NA 264 14.3 79 6 6
## 38 29 127 9.7 82 6 7
## 39 NA 273 6.9 87 6 8
## 40 71 291 13.8 90 6 9
## 41 39 323 11.5 87 6 10
## 42 NA 259 10.9 93 6 11
## 43 NA 250 9.2 92 6 12
## 44 23 148 8.0 82 6 13
## 45 NA 332 13.8 80 6 14
## 46 NA 322 11.5 79 6 15
## 47 21 191 14.9 77 6 16
## 48 37 284 20.7 72 6 17
## 49 20 37 9.2 65 6 18
## 50 12 120 11.5 73 6 19
## 51 13 137 10.3 76 6 20
## 52 NA 150 6.3 77 6 21
## 53 NA 59 1.7 76 6 22
## 54 NA 91 4.6 76 6 23
## 55 NA 250 6.3 76 6 24
## 56 NA 135 8.0 75 6 25
## 57 NA 127 8.0 78 6 26
## 58 NA 47 10.3 73 6 27
## 59 NA 98 11.5 80 6 28
## 60 NA 31 14.9 77 6 29
## 61 NA 138 8.0 83 6 30
## 62 135 269 4.1 84 7 1
## 63 49 248 9.2 85 7 2
## 64 32 236 9.2 81 7 3
## 65 NA 101 10.9 84 7 4
## 66 64 175 4.6 83 7 5
## 67 40 314 10.9 83 7 6
## 68 77 276 5.1 88 7 7
## 69 97 267 6.3 92 7 8
## 70 97 272 5.7 92 7 9
## 71 85 175 7.4 89 7 10
## 72 NA 139 8.6 82 7 11
## 73 10 264 14.3 73 7 12
## 74 27 175 14.9 81 7 13
## 75 NA 291 14.9 91 7 14
## 76 7 48 14.3 80 7 15
## 77 48 260 6.9 81 7 16
## 78 35 274 10.3 82 7 17
## 79 61 285 6.3 84 7 18
## 80 79 187 5.1 87 7 19
## 81 63 220 11.5 85 7 20
## 82 16 7 6.9 74 7 21
## 83 NA 258 9.7 81 7 22
## 84 NA 295 11.5 82 7 23
## 85 80 294 8.6 86 7 24
## 86 108 223 8.0 85 7 25
## 87 20 81 8.6 82 7 26
## 88 52 82 12.0 86 7 27
## 89 82 213 7.4 88 7 28
## 90 50 275 7.4 86 7 29
## 91 64 253 7.4 83 7 30
## 92 59 254 9.2 81 7 31
## 93 39 83 6.9 81 8 1
## 94 9 24 13.8 81 8 2
## 95 16 77 7.4 82 8 3
## 96 78 NA 6.9 86 8 4
## 97 35 NA 7.4 85 8 5
## 98 66 NA 4.6 87 8 6
## 99 122 255 4.0 89 8 7
## 100 89 229 10.3 90 8 8
## 101 110 207 8.0 90 8 9
## 102 NA 222 8.6 92 8 10
## 103 NA 137 11.5 86 8 11
## 104 44 192 11.5 86 8 12
## 105 28 273 11.5 82 8 13
## 106 65 157 9.7 80 8 14
## 107 NA 64 11.5 79 8 15
## 108 22 71 10.3 77 8 16
## 109 59 51 6.3 79 8 17
## 110 23 115 7.4 76 8 18
## 111 31 244 10.9 78 8 19
## 112 44 190 10.3 78 8 20
## 113 21 259 15.5 77 8 21
## 114 9 36 14.3 72 8 22
## 115 NA 255 12.6 75 8 23
## 116 45 212 9.7 79 8 24
## 117 168 238 3.4 81 8 25
## 118 73 215 8.0 86 8 26
## 119 NA 153 5.7 88 8 27
## 120 76 203 9.7 97 8 28
## 121 118 225 2.3 94 8 29
## 122 84 237 6.3 96 8 30
## 123 85 188 6.3 94 8 31
## 124 96 167 6.9 91 9 1
## 125 78 197 5.1 92 9 2
## 126 73 183 2.8 93 9 3
## 127 91 189 4.6 93 9 4
## 128 47 95 7.4 87 9 5
## 129 32 92 15.5 84 9 6
## 130 20 252 10.9 80 9 7
## 131 23 220 10.3 78 9 8
## 132 21 230 10.9 75 9 9
## 133 24 259 9.7 73 9 10
## 134 44 236 14.9 81 9 11
## 135 21 259 15.5 76 9 12
## 136 28 238 6.3 77 9 13
## 137 9 24 10.9 71 9 14
## 138 13 112 11.5 71 9 15
## 139 46 237 6.9 78 9 16
## 140 18 224 13.8 67 9 17
## 141 13 27 10.3 76 9 18
## 142 24 238 10.3 68 9 19
## 143 16 201 8.0 82 9 20
## 144 13 238 12.6 64 9 21
## 145 23 14 9.2 71 9 22
## 146 36 139 10.3 81 9 23
## 147 7 49 10.3 69 9 24
## 148 14 20 16.6 63 9 25
## 149 30 193 6.9 70 9 26
## 150 NA 145 13.2 77 9 27
## 151 14 191 14.3 75 9 28
## 152 18 131 8.0 76 9 29
## 153 20 223 11.5 68 9 30
airquality %>%
select(Ozone, Month) %>%
filter(Month == '5') %>%
arrange(Ozone) -> gm
summary(gm$Ozone)## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.00 11.00 18.00 23.62 31.50 115.00 5
plot(seq_along(gm$Ozone), gm$Ozone)
lines(x = c(1,31), y = c(1,1), col = "blue")
lines(x = c(1,31), y = c(11,11), col = "blue")
lines(x = c(1,31), y = c(18,18), col = "red")
lines(x = c(1,31), y = c(31.5,31.5), col = "blue")
lines(x = c(1,31), y = c(115,115), col = "blue")*Exemplo 1b
airquality %>%
filter(Month == '5') %>%
select(Ozone) %>%
arrange(Ozone) %>%
unlist()-> gm
summary(gm)## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.00 11.00 18.00 23.62 31.50 115.00 5
plot(seq_along(gm), gm)
lines(x = c(1,31), y = c(1,1), col = "blue")
lines(x = c(1,31), y = c(11,11), col = "blue")
lines(x = c(1,31), y = c(18,18), col = "red")
lines(x = c(1,31), y = c(31.5,31.5), col = "blue")
lines(x = c(1,31), y = c(115,115), col = "blue")## [1] "formula"
env_meu <- new.env()
env_meu$Ozo <- airquality$Ozone
env_meu$Mon <- airquality$Month
boxplot(formula = Ozo ~ Mon)## Error in eval(predvars, data, env): object 'Ozo' not found
Ozo <- airquality$Ozone
Mon <- airquality$Month
boxplot(formula = Ozo ~ Mon)
Temp <- airquality$Temp
boxplot(formula = Temp ~ Mon)##
## Call:
## lm(formula = Ozo ~ Wind, data = airquality)
##
## Coefficients:
## (Intercept) Wind
## 96.873 -5.551
par(mfrow=c(1,2))
plot(airquality$Wind, airquality$Ozone)
plot(airquality$Solar.R, airquality$Ozone)plot_ly(data = airquality,
x = ~Wind, y = ~Ozone,
text = ~paste0(Day, '/', Month),
type = 'scatter', mode ='markers') -> p## Warning: Ignoring 37 observations
library(lattice)
state <- data.frame(state.x77,
region = state.region)
xyplot(Life.Exp ~ Income | region,
data = state,
layout = c(4, 1))## Error in shiny::shinyAppDir("dist"): No Shiny application exists at the path "dist"
Proposal: I intend to show in the next lines the resolution of a problem presented in the book: Bussab,W.O. ; Morettin, P.A. Estatística Básica. São Paulo: 2017 Saraiva Educação.
A psychologist is investigating the relationship between the time it takes for an individual to react to a visual stimulus \((y)\) and some factors such as gender \((w)\), age \((x)\), and visual acuity (\(z\), measured as a percentage). The results were tabled, as follows:
n <- c(1:20)
y <- c(96,92,106,100,98,104,110,101,116,106,109,100,112,105,118,108,113,112,127,117)
x <- c(rep(20,4),rep(25,4),rep(30,4),rep(35,4),rep(40,4))
w <- c('h','m','h','m','m','h','h','m','m','h','h','m','m','m','h','h','m','m','h','h')
z <- c(90,100,80,90,100,90,80,90,70,90,90,80,90,80,70,90,90,90,60,80)
my_data <- data.frame(n,y,w,x,z)
knitr::kable(my_data, caption = "Reaction time to a visual stimulus (y) and visual acuity (z) of 20 individuals, by sex (w) and age (x)", align = c('c', 'c', 'c'))| n | y | w | x | z |
|---|---|---|---|---|
| 1 | 96 | h | 20 | 90 |
| 2 | 92 | m | 20 | 100 |
| 3 | 106 | h | 20 | 80 |
| 4 | 100 | m | 20 | 90 |
| 5 | 98 | m | 25 | 100 |
| 6 | 104 | h | 25 | 90 |
| 7 | 110 | h | 25 | 80 |
| 8 | 101 | m | 25 | 90 |
| 9 | 116 | m | 30 | 70 |
| 10 | 106 | h | 30 | 90 |
| 11 | 109 | h | 30 | 90 |
| 12 | 100 | m | 30 | 80 |
| 13 | 112 | m | 35 | 90 |
| 14 | 105 | m | 35 | 80 |
| 15 | 118 | h | 35 | 70 |
| 16 | 108 | h | 35 | 90 |
| 17 | 113 | m | 40 | 90 |
| 18 | 112 | m | 40 | 90 |
| 19 | 127 | h | 40 | 60 |
| 20 | 117 | h | 40 | 80 |
paste("We have the reaction time for n =",length(y), "individuals. (values for random variable y).")## [1] "We have the reaction time for n = 20 individuals. (values for random variable y)."
## [1] "n1 = n2 = 10"
## [1] "n1 = n2 = n3 = n4 = n5 = 4"
Therefore, it was not possible to control the variable \(z\) a priori as the other two, since it requires ophthalmic examinations for its measurement. Hence, the unbalance of the observed sizes. This factor is known as cofactor type.
Figure 1 shows the graphic model of this experiment:
A simple linear regression model can be represented as: \(y_i = α + βx_i + e_i,i = 1,2,...,n\)
The amount of information lost by the model or the sum of squares of errors (or deviations) is given by: \[SQ(α,β)= ∑ (e_i )^2 = ∑ (y_i-(α+ β∙x_i ))^2\]
To find the minimum value of SQ, we must derive SQ with respect to \(α\) and \(β\) and match the derivatives to zero, obtaining:
\[SQ_β= 2∙∑(y_i-α-β∙x_i )∙(-x_i ) = 0\] \[SQ_α= 2∙∑(y_i-α-β∙x_i )∙(-1) = 0\]
Solving this system of equations we get:
\[α ̂= (∑ y_i)/n- β ̂ (∑x_i )/n= y ̅- β ̂∙x ̅\] \[β ̂= (∑x_i y_i-((∑x_i )∙(∑y_i ))/n)/(∑x_i^2-(∑x_i )^2/n)= (∑x_i y_i-n(xy) ̅ /(∑x_i^2-nx ̅^2 )\]
Where, \(α ̂\) and \(β ̂\) are the estimated linear and angular coefficients, respectively.
In our Case Study, we assume:
\(y_i\): reaction time of i-th individual;
\(x_i\): age of i-th individual;
\(e_i\): deviation, $i = 1, 2, …, 20.
\(n = 20\),
\(∑y_i= 2150\),
\(∑i=600\),
\(∑x_i∙y_i=65400\),
\(y ̅=107.5\),
\(x ̅=30\),
\(∑x^2=1900\)
\[(β ) ̂=(65400- (20)(30)(107.5))/(19000-(20) (30)^2 )=0,90\] \[α ̂=107.5-(0,90)(30)=80,5\] What gives us the adjusted model: \[y ̂=80,5+0,9∙x_i,i=1,2,…,20\]
With this model, we can predict, for example, the average reaction time for 33-year-olds, which is an unobserved age group: \[y ̂(33)=80,5+0,9(33)=110,2.\]
The graph illustrated bellow adds to the previous one the line obtained by the calculated linear regression.
plot(formula = y ~ x, main = "lm(data = my_data, formula = y ~ x)")
abline(lm(data = my_data, formula = y ~ x), col = "red")